<template>
  <div class="page-container">
    <div class="card-title" v-show="showSearch">
      <el-form size="small" label-width="68px" :model="queryParams" ref="queryForm">
        <el-row :gutter="48">
          <el-col :md="6" :sm="24">
            <el-form-item label="表格名称">
              <el-input v-model="queryParams.tableName" class="search-item" placeholder="请输入表格名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="模块名称">
              <el-input v-model="queryParams.moduleName" class="search-item" placeholder="请输入模块名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="包名称">
              <el-input v-model="queryParams.packageName" class="search-item" placeholder="请输入包名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24" v-if="advanced">
            <el-form-item label="二级缓存">
              <el-select v-model="queryParams.enableCache" placeholder="请选择是否二级缓存" class="search-item" clearable>
                <el-option
                  v-for="dict in dict.type.sys_switch_enable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24" v-if="advanced">
            <el-form-item label="链式编程">
              <el-select v-model="queryParams.enableChain" placeholder="请选择是否开启链式编程" class="search-item" clearable>
                <el-option
                  v-for="dict in dict.type.sys_switch_enable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24" v-if="advanced">
            <!-- 创建时间 -->
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="queryParams.dataRange"
                class="search-item"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <div class="search-content">
            <!-- 搜索按钮 -->
            <el-form-item class="search-button">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
              <el-button
                type="text"
                :icon="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                class="advanced-button"
                @click="toggleAdvanced">
                {{ advanced ? '关闭' : '展开' }}
              </el-button>
            </el-form-item>
          </div>
        </el-row>
      </el-form>
    </div>
    <div class="card">
      <el-row :gutter="10" class="button-container">
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" plain :disabled="multiple" @click="handleDelete" :loading="buttonLoading">批量删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" :columns="columns" @getTableData="getTableData"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" element-loading-text="正在加载代码生成数据" :data="tableData" @selection-change="handleSelectionChange" border>
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column type="selection" width="50" align="center" />
        <!-- 表格名称 -->
        <el-table-column prop="tableName" label="表格名称" align="center" v-if="columns[0].visible"></el-table-column>
         <!-- 模块名称 -->
         <el-table-column prop="moduleName" label="模块名称" align="center" v-if="columns[1].visible"></el-table-column>
        <!-- 包名称 -->
        <el-table-column prop="packageName" label="包名称" align="center" v-if="columns[2].visible"></el-table-column>
        <!-- 二级缓存 -->
        <el-table-column prop="enableCache" label="二级缓存" align="center" v-if="columns[3].visible">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enableCache">开启</el-tag>
            <el-tag type="danger" v-else>关闭</el-tag>
          </template>
        </el-table-column>
        <!-- 链式编程 -->
        <el-table-column prop="enableChain" label="链式编程" align="center" v-if="columns[4].visible">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enableChain">开启</el-tag>
            <el-tag type="danger" v-else>关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="生成时间" align="center" v-if="columns[5].visible">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime | dateFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-refresh" @click="handleDownload(scope.row.id)">生成</el-button>
            <el-button type="text" size="small" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <pagination
        :background="false"
        :total="total"
        :page.sync="queryParams.currentPage"
        :limit.sync="queryParams.pageSize"
        @pagination="getTableData"
      />
    </div>
  </div>
</template>
<script>
import { saveAs } from 'file-saver'
import Pagination from '../../../../../components/Pagination/index.vue'
import RightToolbar from '../../../../../components/RightToolbar/index.vue'
import { listGenerateLogs, deleteGenerateLog, download } from '../../../../../api/webiste/generate-log'

export default {
  name: 'GenerateLog',

  dicts: ['sys_switch_enable'],

  components: { Pagination, RightToolbar },

  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 是否展示搜索内容
      showSearch: true,
      // 数据总条数
      total: 0,
      // 数据表格等待框
      loading: false,
      // 查询参数
      queryParams: {
        // 页数
        currentPage: 1,
        // 页面大小
        pageSize: 10,
        // 二级缓存
        enableCache: undefined,
        // 链式编程
        enableChain: undefined,
        // 表格名称
        tableName: undefined,
        // 模块名称
        moduleName: undefined,
        // 包名称
        packageName: undefined,
        // 时间范围
        dataRange: []
      },
      // 对话框按钮等待框
      buttonLoading: false,
      // 列信息
      columns: [
        // 表格名称
        { key: 0, label: '表格名称', visible: true },
        // 模块名称
        { key: 1, label: '模块名称', visible: true },
        // 包名称
        { key: 2, label: '包名称', visible: true },
        // 二级缓存
        { key: 3, label: '二级缓存', visible: true },
        // 链式编程
        { key: 4, label: '链式编程', visible: true },
        { key: 5, label: '创建时间', visible: true }
      ],
      // 表格数据
      tableData: [],
      // 表头选中的数据
      ids: [],
      // 多数据禁用
      single: true,
      // 单数据禁用
      multiple: true
    }
  },

  mounted () {
    // 查询表格数据
    this.getTableData()
  },

  methods: {
    // 获取表格数据
    getTableData () {
      // 开启加载框
      this.loading = true
      listGenerateLogs(this.queryParams).then((res) => {
        // 赋值数据参数
        this.tableData = res.list
        this.total = res.total
        // 关闭等待框
        this.loading = false
      }).catch(() => {
        // 确保加载框可以关闭
        this.loading = false
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 点击查询按钮
    handleQuery () {
      this.queryParams.currentPage = 1
      this.getTableData()
    },
    // 点击重置按钮
    handleReset () {
      // 二级缓存
      this.queryParams.enableCache = undefined
      // 链式编程
      this.queryParams.enableChain = undefined
      // 表格名称
      this.queryParams.tableName = undefined
      // 模块名称
      this.queryParams.moduleName = undefined
      // 包名称
      this.queryParams.packageName = undefined
      this.queryParams.dataRange = []
    },
    // 点击删除事件
    handleDelete (row) {
      const that = this
      let generateLogIds = []
      if (row.id) {
        generateLogIds.push(row.id)
      } else {
        generateLogIds = this.ids
      }
      this.$confirm('是否确认删除选中的代码生成日志数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.buttonLoading = true
        that.loading = true
        deleteGenerateLog(generateLogIds).then((res) => {
          that.buttonLoading = false
          that.$message.success('删除成功')
          // 刷新数据
          that.getTableData()
        }).catch(() => {
          // 确保加载框会关闭
          that.buttonLoading = false
          that.loading = false
        })
      }).catch(() => {})
    },
    // 重置表单
    reset () {
      // 清除表单校验
      this.resetForm('generateLogForm')
    },
    // 多选框
    handleSelectionChange (selection) {
      // 获取当前选中数组的ID
      this.ids = selection.map(item => item.id)
      // 表头的编辑是否可以点击
      this.single = selection.length !== 1
      // 表头的删除是否可以点击
      this.multiple = !selection.length
    },
    // 预览代码
    handleDownload (id) {
      download(id).then((res) => {
        const blob = new Blob([res], { type: 'application/zip' })
        saveAs(blob, 'blog.zip')
      })
    }
  }
}

</script>

<style lang="scss" scoped>
.generateLog-wrapper {
  padding: 10px 30px;
}
</style>
